﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Wise.Util
{

    public class JsonHelper : HelperBase<JsonHelper>
    {
        public static JObject JsonToJObject(string jsonString)
        {
            return JsonConvert.DeserializeObject<JObject>(jsonString);
            //return JsonHelper.ToJObject(jsonString);
        }

        public static JObject ToJObject(string jsonString)
        {
            if (string.IsNullOrEmpty(jsonString))
                return new JObject();
            return JsonConvert.DeserializeObject<JObject>(jsonString);
        }

        public static T ToObject<T>(string jsonString)
        {
            if (string.IsNullOrEmpty(jsonString))
                return default(T);
            return JsonConvert.DeserializeObject<T>(jsonString);
        }

        public static object ToObject(string jsonString, Type type)
        {
            if (string.IsNullOrEmpty(jsonString))
                return type.IsValueType ? ObjectHelper.CreateInstance(type) : null;
            return JsonConvert.DeserializeObject(jsonString, type);
        }

        public static string ToJson(object obj)
        {
            if (obj == null)
                return string.Empty;
            return JsonConvert.SerializeObject(obj);
        }
        public static string ToJson_expand(object obj)
        {
            if (obj == null)
                return string.Empty;
            return JsonConvert.SerializeObject(obj,Formatting.Indented);
        }
    }
}
